package com.abc.easyclaim;

import java.util.ArrayList;
import java.util.List;

import com.abc.bean.Case;
import com.abc.bean.Record;
import com.abc.db.DBManager;
import com.abc.tool.StringTool;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;

public class PlaySound extends Activity{

	UIUpdateReceiver uiur;          //界面更新Intent的接收者
	private SeekBar seekBar;           //拖动进度条控件，显示播放进度  
	private ImageButton previousButton;          //播放上一个录音按钮
	private ImageButton playButton;             //播放/暂停按钮
	private ImageButton stopButton;              //停止播放按钮
	private ImageButton nextButton;              //播放下一个按钮
	private Button returnButton;                 //返回按钮
	private int currentSound;                       //当前播放录音序号
	private int seekProgress=0;            //播放进度条值
	private MainApplication appCurrentPlay;            //全局变量
	private DBManager dbManager;           //数据库操作管理器
	private Case caze;
	private List<Record> records=new ArrayList<Record>();         //录音集合
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.playsound);
		appCurrentPlay=(MainApplication)getApplication();
		//标题栏的布局
		//getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.playsound_titlebar); 
		//创建界面更新Intent接收者
		uiur=new UIUpdateReceiver(this);
		//获取组件
		seekBar=(SeekBar)findViewById(R.id.play_seekBar);
		previousButton=(ImageButton)findViewById(R.id.play_previous);
		playButton=(ImageButton)findViewById(R.id.play_sound);
		stopButton=(ImageButton)findViewById(R.id.play_stopsound);
		nextButton=(ImageButton)findViewById(R.id.play_next);
		returnButton=(Button)findViewById(R.id.return_playsound);
		//从数据库中读取录音信息
		dbManager=new DBManager(getBaseContext());
		records=dbManager.queryRecords(MainApplication.getCurrentCaseId());
		caze = dbManager.queryCase(MainApplication.getCurrentCaseId());
		System.out.println("this.caze.getName()="+caze.getName());
	
		//初始化显示
		initConpoment();
		
		//为组件添加监听器
		previousButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				System.out.println("previous_uiur.status="+uiur.status);
				//不是第一个录音文件
				if (currentSound>0) {
					currentSound--;
					Intent intent=new Intent(Constant.MUSIC_CONTROL);
					intent.putExtra("cmd", Constant.COMMAND_PREVIOUS);  
					intent.putExtra("currentSound", currentSound);
					intent.putExtra("cazeId", MainApplication.getCurrentCaseId());
					//发出播放上一个命令
					PlaySound.this.sendBroadcast(intent);
				}
				else {
					Toast.makeText(PlaySound.this, "往上没有录音了！", Toast.LENGTH_SHORT).show();
				}
			}
		});
		playButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				System.out.println("play_uiur.status="+uiur.status);
				//若当前为播放状态
				if (uiur.status==Constant.STATUS_PLAY) {
					Intent intent=new Intent(Constant.MUSIC_CONTROL);
					intent.putExtra("cmd", Constant.COMMAND_PAUSE); 
					intent.putExtra("cazeId", MainApplication.getCurrentCaseId());
					//发出暂停命令
					PlaySound.this.sendBroadcast(intent);     
				}
				//若当前为停止状态
				else if (uiur.status==Constant.STATUS_STOP) {
					System.out.println("currentSound="+currentSound);
					Intent intent=new Intent(Constant.MUSIC_CONTROL);
					intent.putExtra("cmd", Constant.COMMAND_PLAY);  
					intent.putExtra("cazeId", MainApplication.getCurrentCaseId());
					intent.putExtra("currentSound", currentSound);
					//发出播放命令和播放文件序号
					PlaySound.this.sendBroadcast(intent);
				}
				//若当前为暂停状态
				else {
					Intent intent=new Intent(Constant.MUSIC_CONTROL);
					intent.putExtra("cmd", Constant.COMMAND_PLAY); 
					intent.putExtra("cazeId", MainApplication.getCurrentCaseId());
					//发出继续播放命令
					PlaySound.this.sendBroadcast(intent);  
				}
			}
		});
		stopButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				System.out.println("stop_uiur.status="+uiur.status);
				//按下停止按钮后广播出去停止命令的Intent					
				Intent intent=new Intent(Constant.MUSIC_CONTROL);
				intent.putExtra("cmd", Constant.COMMAND_STOP);
				intent.putExtra("cazeId", MainApplication.getCurrentCaseId());
				PlaySound.this.sendBroadcast(intent);
			}
		});
		nextButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				System.out.println("next_uiur.status="+uiur.status);
				//如果不是最后一个录音
				if (currentSound<(records.size()-1)) {
					currentSound++;
					Intent intent=new Intent(Constant.MUSIC_CONTROL);
					intent.putExtra("cmd", Constant.COMMAND_NEXT); 
					intent.putExtra("cazeId", MainApplication.getCurrentCaseId());
					intent.putExtra("currentSound", currentSound);
					//发出播放下一个命令
					PlaySound.this.sendBroadcast(intent);
				}
				else {
					Toast.makeText(PlaySound.this, "已经是最后一个录音文件了！", Toast.LENGTH_SHORT).show();
				}
			}
		});
		seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {  //停止拖动进度条
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub           //开始拖动进度条
				
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub            //当进度条值改变时，从新的位置开始播放音频
				if (fromUser) {
					System.out.println("progress="+progress);
					Intent intent=new Intent(Constant.MUSIC_CONTROL);
					intent.putExtra("cmd", Constant.COMMAND_SETBAR);
					seekProgress=progress;
					intent.putExtra("progress", seekProgress);
					intent.putExtra("cazeId", MainApplication.getCurrentCaseId());
					//发出改变进度条命令
					PlaySound.this.sendBroadcast(intent);
				}
			}
		});
		returnButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
				//返回到录音界面
		    	Intent intent=new Intent(PlaySound.this,RecordSound.class);
		    	appCurrentPlay.setState(currentSound);
		    	startActivity(intent);
			}
		});	
		
		//动态注册接收播放、暂停、停止状态更新Intent的UIUpdateReceiver
        IntentFilter filter=new IntentFilter();
		filter.addAction(Constant.UPDATE_STATUS);
		this.registerReceiver(uiur, filter);	

		//发出启动后台Service的Intent
		this.startService(new Intent(this,MyMusicPlayerService.class));	

	}

	private void initConpoment() {
		//获取RecordSound传过来要播放录音的序号
		currentSound=appCurrentPlay.getState();
		TextView playNametv=(TextView)findViewById(R.id.play_soundname);            //播放文件名称
		TextView playSoundDatetv=(TextView)findViewById(R.id.play_soundtime);      //播放文件的录制日期
		TextView playTimetv=(TextView)findViewById(R.id.play_time);                 //已播放时间长度
		TextView playLongtv=(TextView)findViewById(R.id.play_long);                  //文件播放长度
		TextView playSoundDescriptiontv=(TextView)findViewById(R.id.play_sounddescription);//播放文件的描述信息
		playNametv.setText(records.get(currentSound).getTitle());
		playSoundDatetv.setText("录制于 "+StringTool.date2String(records.get(currentSound).getDate(), "yyyy-MM-dd HH:mm:ss"));
		playTimetv.setText("00:00:00");
		playLongtv.setText(UIUpdateReceiver.getPlayTime(records.get(currentSound).getLength()));
	    seekBar.setMax(records.get(currentSound).getLength());   //设置进度条最大值
		seekBar.setProgress(0);  //设置进度条值
		playSoundDescriptiontv.setText(records.get(currentSound).getDescription());
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		this.unregisterReceiver(uiur);
		super.onDestroy();
	}

}
